package com.yuanfengPlus.supplierExpense.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuanfengPlus.supplierExpense.dao.ISupplierExpenseMapper;
import com.yuanfengPlus.supplierExpense.model.SupplierExpenseInfo;
import com.yuanfengPlus.supplierExpense.model.SupplierPurchaseInfo;
import com.yuanfengPlus.supplierExpense.service.ISupplierExpenseService;
import com.yuanfengPlus.utils.PageResult;
import com.yuanfengPlus.utils.Result;
import com.yuanfengPlus.utils.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SupplierExpenseServiceImpl implements ISupplierExpenseService {
    @Resource
    private ISupplierExpenseMapper isem;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<String> insert(SupplierExpenseInfo supplierExpenseInfo) {
        System.out.println("supplierExpenseInfo:" + supplierExpenseInfo);
        if (supplierExpenseInfo.getSupplier_name() == null)
            return Result.failure(500, "供应商名称不能为空");
        if (supplierExpenseInfo.getSupplierPurchaseInfoList() == null && supplierExpenseInfo.getSupplierPurchaseInfoList().isEmpty())
            return Result.failure(500, "采购信息不能为空");

        // 计算总金额
        double totalAmount = 0.0;
        for (SupplierPurchaseInfo supplierPurchaseInfo : supplierExpenseInfo.getSupplierPurchaseInfoList()) {
            totalAmount += supplierPurchaseInfo.getValue();
        }
        supplierExpenseInfo.setSupplier_expense_total_price(totalAmount);

        // 插入供应商费用信息
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(31);
        supplierExpenseInfo.setSupplier_expense_ID(snowflakeIdWorker.nextId());
        supplierExpenseInfo.setSupplier_expense_operate_time(new java.sql.Timestamp(System.currentTimeMillis()));
        Integer i = isem.insertSupplierExpenseInfo(supplierExpenseInfo);
        if (i <= 0)
            return Result.failure(500, "供应商费用信息插入失败");

        // 批量插入采购信息
        List<SupplierPurchaseInfo> purchaseInfoList = supplierExpenseInfo.getSupplierPurchaseInfoList();
        for (SupplierPurchaseInfo supplierPurchaseInfo : purchaseInfoList) {
            supplierPurchaseInfo.setId(supplierExpenseInfo.getSupplier_expense_ID());
            isem.insertSupplierPurchaseInfo(supplierPurchaseInfo);
        }
        return Result.success("供应商费用信息插入成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PageResult select(Map<String, Object> params) {
        // 检查并设置默认值
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);

        // 确保 pageNum 和 pageSize 不为 null
        if (pageNum == null || pageSize == null) {
            throw new IllegalArgumentException("pageNum 和 pageSize 不能为 null");
        }

        PageHelper.startPage(pageNum, pageSize);

        // 确保 ilocm 不为 null
        if (isem == null) {
            throw new IllegalStateException("ilocm 未正确初始化");
        }

        Page<SupplierExpenseInfo> page = isem.selectAll(params);

        // 确保 page 不为 null
        if (page == null) {
            throw new IllegalStateException("查询结果为空");
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> update(SupplierExpenseInfo supplierExpenseInfo) {
        supplierExpenseInfo.setSupplier_expense_review_time(new java.sql.Timestamp(System.currentTimeMillis()));



        Integer i = isem.deleteSupplierPurchaseInfo(supplierExpenseInfo.getSupplier_expense_ID());
        if (i <= 0)
            return Result.failure(500, "供应商费用信息修改失败");

        // 插入新的供应商采购信息
        List<SupplierPurchaseInfo> supplierPurchaseInfoList = supplierExpenseInfo.getSupplierPurchaseInfoList();
        if (supplierPurchaseInfoList != null && !supplierPurchaseInfoList.isEmpty()) {
            for (SupplierPurchaseInfo supplierPurchaseInfo : supplierPurchaseInfoList) {
                supplierPurchaseInfo.setId(supplierExpenseInfo.getSupplier_expense_ID());
                Integer insertResult = isem.insertSupplierPurchaseInfo(supplierPurchaseInfo);
                if (insertResult <= 0) {
                    return Result.failure(500, "供应商采购信息插入失败");
                }
            }
        }
        // 计算总金额
        double totalAmount = 0.0;
        for (SupplierPurchaseInfo supplierPurchaseInfo : supplierExpenseInfo.getSupplierPurchaseInfoList()) {
            totalAmount += supplierPurchaseInfo.getValue();
        }
        supplierExpenseInfo.setSupplier_expense_total_price(totalAmount);

        Integer update = isem.update(supplierExpenseInfo);
        if (update <= 0)
            return Result.failure(500, "供应商费用信息修改失败");
        return Result.success("供应商费用信息修改成功");
    }

    @Override
    public Result<SupplierExpenseInfo> selectById(String supplier_expense_ID) {
        SupplierExpenseInfo supplierExpenseInfo = isem.selectOne(supplier_expense_ID);
        if (supplierExpenseInfo == null) {
            return Result.failure(500, "供应商费用信息为空");
        }
        return Result.success(supplierExpenseInfo);
    }
}


